using B.WMS.GoodsDto.Input;
using B.WMS.GoodsDto.Output;
using B.WMS.Good;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace B.WMS.Controllers
{
    /// <summary>
    /// 预测控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class PredictionController : WMSController
    {
        private readonly IGoodsAppService _goodsAppService;

        public PredictionController(IGoodsAppService goodsAppService)
        {
            _goodsAppService = goodsAppService;
        }

        /// <summary>
        /// 获取商品需求预测
        /// </summary>
        /// <param name="input">预测输入参数</param>
        /// <returns>预测结果</returns>
        [HttpPost("goods-prediction")]
        public async Task<ActionResult<PredictionResultDto>> GetGoodsPrediction([FromBody] PredictionInputDto input)
        {
            try
            {
                // 验证输入参数
                if (input.StartDate >= input.EndDate)
                {
                    return BadRequest("开始时间必须早于结束时间");
                }

                if (input.StartDate < DateTime.Now.Date)
                {
                    return BadRequest("开始时间不能早于今天");
                }

                // 调用预测服务
                var result = await _goodsAppService.GetGoodsPrediction(input);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "预测过程中发生错误", message = ex.Message });
            }
        }

        /// <summary>
        /// 获取商品需求预测（简化版本）
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="goodsCoding">商品编码（可选）</param>
        /// <returns>预测结果</returns>
        [HttpGet("goods-prediction-simple")]
        public async Task<ActionResult<PredictionResultDto>> GetGoodsPredictionSimple(
            [FromQuery] DateTime startDate,
            [FromQuery] DateTime endDate,
            [FromQuery] string? goodsCoding = null)
        {
            try
            {
                var input = new PredictionInputDto
                {
                    StartDate = startDate,
                    EndDate = endDate,
                    GoodsCoding = goodsCoding,
                    IntervalDays = 1,
                    IncludeHistoricalData = true,
                    HistoricalDataDays = 30
                };

                var result = await _goodsAppService.GetGoodsPrediction(input);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "预测过程中发生错误", message = ex.Message });
            }
        }
    }
} 